/*
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-07-11 20:01:53
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-08-07 10:19:26
 * @FilePath: /XMate-h5/src/store/modules/minutes/utils/userNameResolver.ts
 * @Description: 
 * 
 * Copyright (c) 2025 by ${git_name_email}, All Rights Reserved. 
 */
import {
  UidInfo,
  CustomerRoles,
  ParticipantResult,
  InternalPartner
} from "@/types/minutes/store.type";

/**
 * 根据权重规则获取用户名
 * 权重规则：CustomerRoles > ParticipantResult > UidInfo
 * @param ui 用户ID
 * @param uidInfo UidInfo映射
 * @param customerRoles 客户角色数据
 * @param participantResults 参与者结果数据
 * @returns 用户名
 */
export function getUserName(
  ui: string,
  uidInfo: Record<string, UidInfo> = {},
  customerRoles: CustomerRoles[] = [],
  internalPartners: InternalPartner[] = [],
  participantResults: ParticipantResult[] = []
): string {
  let userName = uidInfo[ui]?.name || "-";

  const customerRole = customerRoles.find(role => role.ui === ui);
  if (customerRole?.name) {
    userName = customerRole.name;
    return userName;
  }

  const internalPartner = internalPartners.find(role => role.ui === ui);
  if (internalPartner?.name) {
    userName = internalPartner.name;
    return userName;
  }

  const participantResult = participantResults.find(
    participant => participant.id === ui
  );
  if (participantResult?.value) {
    userName = participantResult.value;
  }
  return userName;
}
